package top.sssd.auth.controller.permission;


import java.time.LocalDateTime;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;
import top.sssd.auth.common.CommonResponse;
import top.sssd.auth.model.permission.request.PermissionQueryReq;
import top.sssd.auth.model.permission.request.PermissionSaveOrUpdateReq;
import top.sssd.auth.model.permission.response.PermissionResp;
import top.sssd.auth.service.permission.PermissionService;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;


@Api(tags = "权限表管理")
@RestController
@RequestMapping("/permission")
public class PermissionController {

    @Resource
    private PermissionService permissionService;

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询权限表", httpMethod = "GET")
    public CommonResponse<PermissionResp> getById(@PathVariable Serializable id) {
        return CommonResponse.ok(permissionService.getPermissionRespById(id));
    }

    @GetMapping
    @ApiOperation(value = "分页条件查询权限表", httpMethod = "GET")
    public CommonResponse<Page<PermissionResp>> page(PermissionQueryReq permissionQueryReq) {
        return CommonResponse.ok(permissionService.queryPage(permissionQueryReq));
    }

    @PostMapping
    @ApiOperation(value = "新增一条数据到权限表", httpMethod = "POST")
    public CommonResponse<Boolean> save(@RequestBody PermissionSaveOrUpdateReq permissionSaveOrUpdateReq) {
        Boolean successFlag = permissionService.savePermissionReq(permissionSaveOrUpdateReq);
        return CommonResponse.ok(successFlag, successFlag ? "success" : "fail");
    }

    @PutMapping
    @ApiOperation(value = "更新一条数据到权限表", httpMethod = "PUT")
    public CommonResponse<Boolean> update(@RequestBody PermissionSaveOrUpdateReq permissionSaveOrUpdateReq) {
        Boolean successFlag = permissionService.updatePermissionReq(permissionSaveOrUpdateReq);
        return CommonResponse.ok(successFlag, successFlag ? "success" : "fail");
    }

    @PatchMapping
    @ApiOperation(value = "更新部分数据到权限表", httpMethod = "PATCH")
    public CommonResponse<Boolean> patch(@RequestBody PermissionSaveOrUpdateReq permissionSaveOrUpdateReq) {
        Boolean successFlag = permissionService.updatePermissionReq(permissionSaveOrUpdateReq);
        return CommonResponse.ok(successFlag, successFlag ? "success" : "fail");
    }

    @DeleteMapping("/{ids}")
    @ApiOperation(value = "根据多个ID删除权限表", httpMethod = "DELETE")
    public CommonResponse<Boolean> delete(@PathVariable List<Long> ids) {
        Boolean successFlag = permissionService.removeByIds(ids);
        return CommonResponse.ok(successFlag, successFlag ? "success" : "fail");
    }
}
